import { Component } from '@angular/core';

import { NavController } from 'ionic-angular';

import { Camera } from 'ionic-native';
import { PhotoViewer } from 'ionic-native';
import { AlertController } from 'ionic-angular';

@Component({
  selector: 'page-demo',
  templateUrl: 'demo.html'
})
export class DemoPage {
  constructor(public navCtrl: NavController, public alertCtrl: AlertController) {

  }

  openCamera() {
    var srcType = Camera.PictureSourceType.CAMERA;
    var options = this.setOptions(srcType);
    Camera.getPicture(options).then((imageData) => {
      PhotoViewer.show(imageData, 'My image title', { share: false });
    }, (err) => {
      let alert = this.alertCtrl.create({
        title: 'Error',
        subTitle: err,
        buttons: ['OK']
      });
      alert.present();
    });
  }


  selectPic() {
    var srcType = Camera.PictureSourceType.PHOTOLIBRARY;
    var options = this.setOptions(srcType);
    Camera.getPicture(options).then((imageData) => {
      PhotoViewer.show(imageData, 'My image title', { share: false });
    }, (err) => {
      let alert = this.alertCtrl.create({
        title: 'Error',
        subTitle: err,
        buttons: ['OK']
      });
      alert.present();
    });
  }

  setOptions(srcType) {
    var options = {
      // Some common settings are 20, 50, and 100
      quality: 50,
      destinationType: Camera.DestinationType.FILE_URI,
      // In this app, dynamically set the picture source, Camera or photo gallery
      sourceType: srcType,
      encodingType: Camera.EncodingType.JPEG,
      mediaType: Camera.MediaType.PICTURE,
      allowEdit: false,
      correctOrientation: true  //Corrects Android orientation quirks
    }
    return options;
  }

  previewPic() {
    let url = 'http://img1.imgtn.bdimg.com/it/u=3155280771,606419104&fm=21&gp=0.jpg';
    PhotoViewer.show(url, 'My image title', { share: false });
    console.log(url);
  }

}
